package com.mzy.linear_struct.subject;

import java.util.LinkedList;

/**
 * @Author: codermzy
 * @Date: 2024/04/25/下午9:43
 * @Description:
 */
public class 删除链表的倒数第N个结点19 {


    static class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {

            if (head.next == null && n > 0) {
                return null;
            }

            ListNode dummy = new ListNode(0, head);


            ListNode slow = dummy;
            ListNode fast = dummy;
            // f
            // 0 1,2,3,4,5
            // s

            // f
            // 0 1 2
            // s

            for (int i = 0; i < n + 1; i++) {
                fast = fast.next;
            }

            while (fast != null) {
                slow = slow.next;
                fast = fast.next;
            }

            slow.next = slow.next.next;

            return dummy.next;
        }
    }
}
